package com.ebook.ui.fragment.home.everyrecommend;

import com.ebook.entity.Book;
import com.ebook.ui.fragment.Fragment;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;

public class EveryEliteChooseFragment extends Fragment {
    private Hashtable<String, ArrayList<Book>> books;
    private ArrayList<String> categories;
    private JPanel bookDetail;
    private ArrayList<JLabel> labels;
    private int paintedLabel;
    public EveryEliteChooseFragment(){
        super();
        super.serializeId = 11002;
        this.setSize(810,250);
        this.setLocation(364,510);
        getRecommendBookTable();
        bookDetail = new JPanel();
        bookDetail.setLayout(null);
        bookDetail.setLocation(0,50);
        bookDetail.setSize(810,200);
        bookDetail.setBackground(Color.WHITE);
        this.add(bookDetail);
        initComponents();

    }
    public Hashtable<String, ArrayList<Book>> getRecommendBookTable(){
        Hashtable<String, ArrayList<Book>> bks = new Hashtable<>();
        Book a = new Book(100,"计算机组成原理","王晨旭");
        Book b = new Book(101,"git指南","廖天龙");
        Book c = new Book(102,"我的退休生活","张浩仁");
        Book f = new Book(104,"俄罗斯飞机","廖天龙");
        Book g = new Book(105,"飞机上看海","张浩仁");
        ArrayList<Book> bns = new ArrayList<>();
        bns.add(a);
        bns.add(b);
        bns.add(c);
        bns.add(f);
        bns.add(g);
        bks.put("科技",bns);
        ArrayList<Book> bb = (ArrayList<Book>) bns.clone();
        bns.remove(0);
        bks.put("人文", bb);//和其他不一样,问题: 展示的分类没有按照特定顺序
        bks.put("生活",bns);
        bks.put("健康",bns);
        bks.put("植物",bns);
        bks.put("动物世界",bns);
        this.books = bks;
        Enumeration<String> category = books.keys();
        categories = new ArrayList<>();
        while(category.hasMoreElements()){
            categories.add(category.nextElement());
        }
        return bks;
    }
    public ArrayList<Book> getRecommendBookByBookCategory(String category){
        return this.books.get(category);
    }
    private void syncBooks(int index){
        List<Book> bookList = books.get(categories.get(index));
        sync(bookList);
        paintGray(index);
    }
    private void syncBooks(List<Book> bookList){
        sync(bookList);
    }

    private void sync(List<Book> bookList) {
        bookDetail.removeAll();
        bookDetail.setLayout(null);
        int size = bookList.size();
        int gap = (810-154*size)/(size-1);
        int acc_gap = 0;
        for(Book book:bookList){
            BookSummary bs = new BookSummary(book);
            bs.setLocation(acc_gap,0);
            acc_gap = acc_gap+gap+154;
            this.bookDetail.add(bs);
        }
    }

    private void paintGray(int index){
        if(index!=paintedLabel){
            labels.get(paintedLabel).setBackground(Color.WHITE);
            labels.get(paintedLabel).setForeground(Color.orange);
            paintedLabel = index;
            labels.get(paintedLabel).setBackground(Color.orange);
            labels.get(paintedLabel).setForeground(Color.WHITE);
        }
    }
    private void paintGray(String label){
        for(int i = 0;i<labels.size();i++){
            if(labels.get(i).getText().equals(label)){
                paintGray(i);
                return;
            }
        }
        // 回滚
        paintGray(0);
        syncBooks(0);
    }
    @Override
    public void initComponents(){
        Enumeration<String> category = books.keys();
        int acc_gap = 0;
        labels = new ArrayList<>();
        while (category.hasMoreElements()){
            JLabel l = new JLabel(category.nextElement(),SwingConstants.CENTER);
            l.setBackground(Color.white);
            l.setForeground(Color.orange);
            l.setOpaque(true);
            l.addMouseListener(new MouseAdapter(){
                @Override
                public void mouseClicked(MouseEvent e){
                    syncBooks(books.get(l.getText()));
                    System.out.println(l.getText());
                    paintGray(l.getText());
                    EveryEliteChooseFragment.this.repaint();
                }
                @Override
                public void mouseEntered(MouseEvent e){
                    setCursor(new Cursor(Cursor.HAND_CURSOR));
                }
            });
            l.setBounds(acc_gap,0,135,50);
            acc_gap += 135;
            labels.add(l);
            this.add(l);
        }
        paintGray(0);
        syncBooks(0);
    }
}
